package net.microfigure.service.impl;

import net.microfigure.model.FamilyInfo;
import net.microfigure.model.IllCard;
import net.microfigure.model.PartInst;
import net.microfigure.model.ZcPolicy;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.mapper.IllCardMapper;
import net.microfigure.service.IFamilyInfoService;
import net.microfigure.service.IIllCardService;
import net.microfigure.service.IPartInstService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 慢性病证表 服务实现类
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-25
 */
@Service
public class IllCardServiceImpl extends ServiceImpl<IllCardMapper, IllCard> implements IIllCardService {


	@Autowired
	private IFamilyInfoService familyInfoService;

	@Autowired
	private IPartInstService partInstService;

	@Autowired
	private IllCardMapper illCardMapper;


	@Override
	public void deleteIllCard(Long id) {

		IllCard illCard = illCardMapper.selectById(id);
		List<FamilyInfo> list = familyInfoService.selectListByCardNo(illCard.getCardNo());
		if(!CollectionUtils.isEmpty(list)) {
			FamilyInfo familyInfo = list.get(0);
			familyInfo.setIllCardNo("0");
			familyInfoService.updateById(familyInfo);
		}
		this.deleteById(illCard);


	}

	@Override
	public void registerIllCard(IllCard illCard) {
		List<FamilyInfo> list = familyInfoService.selectListByCardNo(illCard.getCardNo());
		if(!CollectionUtils.isEmpty(list)) {
			FamilyInfo familyInfo = list.get(0);
			familyInfo.setIllCardNo(illCard.getIllCardNo());
			familyInfoService.updateById(familyInfo);
		}
		this.insert(illCard);
	}
	/**
	 * 慢性病证添加前数据处理
	 * @param cardNo
	 * @return
	 */
	@Override
	public IllCard perAddIllCard(String cardNo) {
		System.out.println("String cardNo==="+cardNo);
		List<FamilyInfo> list = familyInfoService.selectListByCardNo(cardNo);
		System.out.println("打印familyInfo=="+list.get(0));
		IllCard illCard = new IllCard();
		if (!CollectionUtils.isEmpty(list)) {
			FamilyInfo familyInfo = list.get(0);
			BeanUtils.copyProperties(familyInfo, illCard);
			System.out.println("打印未查询前的familyInfo.getFamilyInfoNo()"+familyInfo.getFamilyInfoNo());
			List<PartInst> list2 = partInstService.selectListByFamilyInfoNo(familyInfo.getFamilyInfoNo());
			System.out.println("打印查询出来的"+list2.get(0));
			if (!CollectionUtils.isEmpty(list2)) {
				PartInst partInst = list2.get(0);
				illCard.setPartNo(partInst.getPartNo());
				illCard.setIllCardNo(getIllCardNo(partInst.getPartNo()));
			}
			
		}System.out.println("返回的慢性病证号"+illCard.getIllCardNo());
			return illCard;

		}

	
	
	/**
	 * 生成慢性病证件号
	 * 
	 * @param preFix
	 * @return
	 */
		@Override
		public String getIllCardNo(String preFix) {
			
			return illCardMapper.sQLQuery(preFix);
		}

	@Override
	public void selectDataGrid(PageInfo pageInfo) {
		Page<IllCard> page = new Page<IllCard>(pageInfo.getNowpage(), pageInfo.getSize());
		EntityWrapper<IllCard> wrapper = new EntityWrapper<IllCard>();
		wrapper.orderBy(pageInfo.getSort(),pageInfo.getOrder().equalsIgnoreCase("ASC"));
		selectPage(page,wrapper);
		pageInfo.setRows(page.getRecords());
		pageInfo.setTotal(page.getTotal());
		
	}

	@Override
	public List<IllCard> selectListByCardNo(String cardNo) {
		
		return illCardMapper.selectListByCardNo(cardNo);
	}

	@Override
	public void selectDataGrid(PageInfo pageInfo, String cardNo) {
		Page<IllCard> page = new Page<IllCard>(pageInfo.getNowpage(), pageInfo.getSize());
		EntityWrapper<IllCard> wrapper = new EntityWrapper<IllCard>();
		System.out.println("输出当前"+cardNo);
		wrapper.where("card_no = {0}", cardNo);
		wrapper.orderBy(pageInfo.getSort(),pageInfo.getOrder().equalsIgnoreCase("ASC"));
		selectPage(page,wrapper);
		pageInfo.setRows(page.getRecords());
		pageInfo.setTotal(page.getTotal());
		
	}

	@Override
	public List<IllCard> equalsByRecodTime(Date recodTime) {
		return illCardMapper.equalsByRecodTime(recodTime);
		// TODO Auto-generated method stub
		
	}

	}
